package com.auxgroup.hr.front.service.personnal.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import com.auxgroup.hr.front.commons.Constant;
import com.auxgroup.hr.front.commons.config.BusinessConfig;
import com.auxgroup.hr.front.commons.utils.File2Byte;
import com.auxgroup.hr.front.dao.domain.EmplPhotoPrimary;
import com.auxgroup.hr.front.dao.domain.PsEmplPhoto;
import com.auxgroup.hr.front.dao.repository.PsEmplPhotoRepository;
import com.auxgroup.hr.front.service.personnal.EmplPhotoService;

/** 
 * @author lvliuhao
 * @version 创建时间：180526 
 * 工作历史信息service
 */
@Service("emplPhotoService")
public class EmplPhotoServiceImpl implements EmplPhotoService {
	private static final Logger logger = LoggerFactory.getLogger(EmplPhotoServiceImpl.class);
	
	@Autowired
	private PsEmplPhotoRepository PsEmplPhotoRepository;
	
	@Autowired
	private BusinessConfig businessConfig;


	@Override
	public PsEmplPhoto getEmplPhoto(EmplPhotoPrimary emplPhotoPrimary) {
		return PsEmplPhotoRepository.findOne(emplPhotoPrimary);
	}


	@Override
	public PsEmplPhoto save(PsEmplPhoto EmplPhoto) {
		return PsEmplPhotoRepository.save(EmplPhoto);
	}


	@Override
	public Boolean delete(EmplPhotoPrimary emplPhotoPrimary) {
		try {
			PsEmplPhotoRepository.delete(emplPhotoPrimary);
		} catch (Exception e) {
			logger.error("delete EmplPhoto error: emplPhotoPrimary={}",emplPhotoPrimary,e);
			return false;
		}
		return true;
	}
	
	
	@Override
	public List<PsEmplPhoto> getAll(String emplid) {
		PsEmplPhoto EmplPhoto = new PsEmplPhoto();
		EmplPhoto.setEmplid(emplid);
		return PsEmplPhotoRepository.findAll(Example.of(EmplPhoto));
	}


	/* (non-Javadoc)
	 * @see EmplPhotoService#buildEmplPhoto(PsEmplPhoto)
	 */
	@Override
	public String buildEmplPhoto(PsEmplPhoto EmplPhoto) {
		StringBuilder jpgPath = new StringBuilder(businessConfig.getEmplePhotoResourceLocation());
		StringBuilder jpgName = new StringBuilder(EmplPhoto.getEmplid());
		jpgName.append(EmplPhoto.getPhotoSizename());
		jpgName.append(".jpg");
		jpgPath.append(jpgName);
		Boolean isSucc = File2Byte.byte2image(EmplPhoto.getEmployeePhoto(), jpgPath.toString(),true);
		if(isSucc){
			return Constant.EMPLPHOTO_STATIC_PATH+jpgName.toString();
		}
		return null;
	}


	
	
	
}
